﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class DiaryproductsDAO:IData
 	{
		public DiaryproductsDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Diaryproducts item, int t) {
			 return connec.ExecuteNonQuery("sp_Diaryproducts", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Diaryproducts.c_id, item.id), new SqlParameter("@" + Diaryproducts.c_productId, item.productId),
				 new SqlParameter("@" + Diaryproducts.c_price, item.price), new SqlParameter("@" + Diaryproducts.c_lastUpdate, item.lastUpdate),
				 new SqlParameter("@" + Diaryproducts.c_isActive, item.isActive)); 
		}

		public  Diaryproducts GetItem(IDataReader dr)
		{
			Diaryproducts obj = new Diaryproducts();
			obj.id = Convert.ToInt64(dr[Diaryproducts.c_id]);
			obj.productId = Convert.ToInt64(dr[Diaryproducts.c_productId]);
			obj.price = Convert.ToDecimal(dr[Diaryproducts.c_price]);
			obj.lastUpdate = Convert.ToDateTime(dr[Diaryproducts.c_lastUpdate]);
			obj.isActive = Convert.ToInt32(dr[Diaryproducts.c_isActive]);
            obj.productName = dr[Diaryproducts.c_productName].ToString();
			return obj;
		}

		public  List<Diaryproducts> GetAll()
		{
            return GetAll("sp_getdiaryProducts", CommandType.StoredProcedure);
		}

		public  Diaryproducts GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}
        private List<Diaryproducts> GetAll(String strSQL, CommandType commandtype)
        {
            List<Diaryproducts> list = new List<Diaryproducts>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
	}

 }
